package com.hdhere.application.controller.admin;

import com.hdhere.application.service.BasicDataService;
import com.hdhere.application.service.VideoService;
import com.hdhere.domain.common.Pagination;
import com.hdhere.domain.common.rest.CallResult;
import com.hdhere.domain.media.Video;
import com.hdhere.domain.media.page.VideoPagination;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Base64;
import java.util.Map;

@Api(tags = "视频文件")
@Controller("VideoAdminController")
@RequestMapping("/admin/video")
public class VideoController {
    @Autowired
    private VideoService videoService;

    @Autowired
    private BasicDataService basicDataService;

    @GetMapping
    public String list(Map<String, Object> map){
        //发布状态数据
        var rowStateList = basicDataService.getEnumList("RowState", new String[]{"10"}, false);
        map.put("states", rowStateList);
        return "admin/videoList";
    }
    @ApiOperation(value = "分页列表")
    @PostMapping
    @ResponseBody
    public Pagination listPage(VideoPagination input){
        input.matchPage();
        Pagination page = videoService.getPage(input);
        return page;
    }

    //@GetMapping({"/save/{id}","/save"})
    @GetMapping({"/save"})
    public String save(@RequestParam(required = false) String id, Map<String, Object> map){

        var video = StringUtils.isBlank(id) ? new Video()
                : videoService.getById(Base64.getDecoder().decode(id));
        map.put("m", video);
        return "admin/videoSave";
    }

    @ApiOperation(value = "保存")
    @PostMapping(value = "/save", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public CallResult Save(@RequestBody Video video){
        videoService.save(video);
        return new CallResult(video.getId());
    }

}
